package com.newshub.admin.service.impl;

import com.newshub.business.dto.PageResult;
import com.newshub.admin.dto.RssFeedQueryDTO;
import com.newshub.admin.entity.RssFeed;
import com.newshub.admin.mapper.RssFeedMapper;
import com.newshub.admin.service.RssFeedService;
import com.newshub.logging.annotation.BusinessLog;
import com.newshub.logging.annotation.LogLevel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class RssFeedServiceImpl implements RssFeedService {

    @Autowired
    private RssFeedMapper rssFeedMapper;

    @Override
    @BusinessLog(module = "RSS源管理", description = "分页查询RSS源列表", level = LogLevel.INFO)
    public PageResult<RssFeed> page(RssFeedQueryDTO queryDTO) {
        int pageNum = queryDTO.getPageNum() == null || queryDTO.getPageNum() < 1 ? 1 : queryDTO.getPageNum();
        int pageSize = queryDTO.getPageSize() == null || queryDTO.getPageSize() < 1 ? 10 : queryDTO.getPageSize();
        int offset = (pageNum - 1) * pageSize;

        List<RssFeed> list = rssFeedMapper.selectByPage(offset, pageSize, queryDTO.getName(), queryDTO.getStatus(), queryDTO.getCategoryId(), queryDTO.getRegionId());
        long total = rssFeedMapper.countByPage(queryDTO.getName(), queryDTO.getStatus(), queryDTO.getCategoryId(), queryDTO.getRegionId());
        return PageResult.of(pageNum, pageSize, total, list);
    }

    @Override
    @BusinessLog(module = "RSS源管理", description = "根据ID查询RSS源", level = LogLevel.INFO)
    public RssFeed getById(Integer id) {
        return rssFeedMapper.selectById(id);
    }

    @Override
    @BusinessLog(module = "RSS源管理", description = "创建RSS源", level = LogLevel.INFO)
    public boolean create(RssFeed feed) {
        return rssFeedMapper.insert(feed) > 0;
    }

    @Override
    @BusinessLog(module = "RSS源管理", description = "更新RSS源", level = LogLevel.INFO)
    public boolean update(RssFeed feed) {
        return rssFeedMapper.update(feed) > 0;
    }

    @Override
    @BusinessLog(module = "RSS源管理", description = "软删除RSS源", level = LogLevel.WARN)
    public boolean softDelete(Integer id) {
        return rssFeedMapper.softDelete(id) > 0;
    }

    @Override
    @BusinessLog(module = "RSS源管理", description = "更新RSS源状态", level = LogLevel.INFO)
    public boolean updateStatus(Integer id, Integer status) {
        return rssFeedMapper.updateStatus(id, status) > 0;
    }

    @Override
    @BusinessLog(module = "RSS源管理", description = "查询启用的RSS源列表", level = LogLevel.INFO)
    public List<RssFeed> listEnabled() {
        // 复用分页接口：仅启用状态，取较大页
        return rssFeedMapper.selectByPage(0, Integer.MAX_VALUE, null, 1, null, null);
    }
    
    @Override
    @BusinessLog(module = "RSS源管理", description = "批量获取RSS源名称映射", level = LogLevel.INFO)
    public Map<Long, String> getNamesByIds(List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return Map.of();
        }
        
        List<RssFeed> feeds = rssFeedMapper.selectByIds(ids);
        return feeds.stream()
                .collect(Collectors.toMap(
                    feed -> feed.getId().longValue(),
                    RssFeed::getName
                ));
    }
}




